/**
 * related to add.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-01
 */
var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");
var rootview = ui("$");
var nf = sm("do_Notification");
var linearLayout_main = ui("do_LinearLayout_main");
var do_Button = ui("do_Button_2");
var aLayout_img = ui("aLayout_img");
var do_ImageView_img = ui("do_ImageView_img");
var do_LinearLayout_items = ui("do_LinearLayout_items");
var communityUrl = require("url/community");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");
var imgTool = require("helper/picture");
var common = require("common");

var param = {};
var newImg = false;
//andriod返回键
page.on("back", function() {
    app.closePage();
});

//点击-相机
/*do_ALayout_camera.on("touch", function(data, e) {
	imgTool.selectImg(1,640,300,100,false,selectImgBack);
});

function selectImgBack(imgList){
	do_ImageView_img.source=imgList[0];
	newImg = true;
}*/
var fileDomain = "";
var oldImg;
var imgMax = 3;
var imgMax2=3;
/** **************处理-图片显示UI-end**************** */
var do_ALayout_img = ui("do_ALayout_img");
var imgGridUI = do_ALayout_img.add("imgGridShower",
		"source://view/public/imgselect/imgGrid.ui", 20, 15);
var imgGridView = ui(imgGridUI + ".imgGrid");
var listdataImg = mm("do_ListData");
imgGridView.bindItems(listdataImg);
imgGridView.on("touch", function(data, e) {
	if(imgMax2<imgMax){
		imgMax2=imgMax2+1;
	}
	listdataImg.removeData([ data ]);
	imgGridView.refreshItems();
});
/** **************处理-图片显示UI-end**************** */
/** **************点击-添加图片-begin**************** */
do_ImageView_img.on("touch", function(data, e) {
	if (listdataImg.getCount() >= imgMax) {
		nf.toast("不能多于" + imgMax + "张图片");
		return;
	}
	imgTool.imgMulti(imgMax2, selectImgBack);// 选择多张图片
});

function selectImgBack(imgList) {
	imgMax2=imgMax2-imgList.length;
	//deviceone.print("------imgList--------"+imgList);
	listdataImg.addData(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.refreshItems();
}
/** **************点击-添加图片-end**************** */

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "编辑技能"
})
header.refreshData();
/*内容*/
var addtxtRemark = linearLayout_main.add("txtRemark", "source://view/reuse/form/mutiTextarea.ui",0,0);
var txtRemark = ui("txtRemark");
txtRemark.bindData(hashdata);
hashdata.addData({
    "textAreaHint" : "技能介绍"
});
txtRemark.refreshData();
var txtRemarkText = ui(addtxtRemark+".textArea");
/*社区*/
var addtxtCommunity = linearLayout_main.add("txtCommunity", "source://view/reuse/form/mutiLabel.ui",0,0);
var txtCommunity = ui("txtCommunity");
txtCommunity.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "社区",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
txtCommunity.refreshData();
var addPickerCommunity = rootview.add("pickerCommunity","source://view/reuse/form/oneCategory.ui", 0, 0);
txtCommunity.on("touch","",300,function(){
	page.hideKeyboard();
	var ptitle = {"title" : "选择社区","id" : "communitySelect"};
	qqHttp.post(communityUrl.url.join.joinList, {}, function(data) {
		var communityList = data.communityList;
		if(communityList==null || communityList.length==0){
			nf.alert("您还没有加入任何一个社区", "提示");
		}else{
			var pcount = [ ptitle, communityList ];
			page.fire("oneCategoryList", pcount);
			page.fire("oneCategory", "true");
		}
	})
});
//选择社区或公开性--响应
page.on("refreshOneCategory", function(data) {
	if ("communitySelect" == data[1]) {//选择社区
		ui(addtxtCommunity+".labelText").text = data[0].onename;
		param.communityId = data[0].oneid;
	}
});
/*类别*/
var addtxtCategory = linearLayout_main.add("txtCategory", "source://view/reuse/form/mutiLabel.ui",0,0);
var txtCategory = ui("txtCategory");
txtCategory.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "分类",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
txtCategory.refreshData();
txtCategory.on("touch","",300,function(){
	page.hideKeyboard();
});

var addLine = linearLayout_main.add("line", "source://view/reuse/form/line.ui",0,0);
var line = ui("line");
line.refreshData();
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var data = page.getData();
	param.id = data.id;
	getData(data.id);
});
var typeParentId = 0;
var addItems = [];
function getData(id) {
	qqHttp.post(homemakingUtil.url.base.toEdit, {id:id}, function(data) {
		//deviceone.print(JSON.stringify(data));
		var vo = data.homemakingBaseVo;
		typeParentId = data.typeParentId;
		if (vo.typeId==homemakingUtil.typeId.SongShui) {
			hashdata.addData({
			    "textAreaHint" : "简单介绍下您的水店，地址"
			});
			txtRemark.refreshData();
			ui("label_imgText").text = "上传您的水店图标";
		} else if (vo.auditStatus == 2) {
			aLayout_img.visible = false;
			do_ImageView_img.enabled = false;
		}
		fileDomain = vo.fileDomain;
		ui(addtxtCommunity+".labelText").text = vo.communityName;
		txtRemarkText.text = vo.name;
		ui(addtxtCategory+".labelText").text = vo.typeStr;
		var list = data.pubitemList;
		for(var i=list.length-1; i >= 0; i--) {
			if (typeParentId == homemakingUtil.typeId.JiaDianWeiXiu) { //家电维修免费
				var addItem = do_LinearLayout_items.add("item"+i, "source://view/homemaking/manage/itemRow.ui", 0, 0);
				var do_CheckBox_1 = ui(addItem+".do_CheckBox_1");
				var label_price = ui(addItem+".label_price");
				do_CheckBox_1.text = list[i].name + " 免费上门";
				for(var j=0; j<vo.homemakingItemVo.length; j++) {
					if (list[i].id == vo.homemakingItemVo[j].itemId) {
						do_CheckBox_1.checked = true;
						break;
					}
				}
				addItems.push({itemId:list[i].id,addItemUi:addItem});
			} else {
				var addItem = do_LinearLayout_items.add("item"+i, "source://view/homemaking/manage/itemRow2.ui", 0, 0);
				var do_CheckBox_1 = ui(addItem+".do_CheckBox_1");
				var do_Label_2 = ui(addItem+".do_Label_2");
				var do_TextField_price = ui(addItem+".do_TextField_price");
				do_CheckBox_1.text = list[i].name;
				do_Label_2.text = "元/" + list[i].unit;
				for(var j=0; j<vo.homemakingItemVo.length; j++) {
					if (list[i].id == vo.homemakingItemVo[j].itemId) {
						do_CheckBox_1.checked = true;
						do_TextField_price.text = vo.homemakingItemVo[j].price;
						break;
					}
				}
				addItems.push({itemId:list[i].id,addItemUi:addItem});
			}
		}
		var imgs = vo.img.split(";");
		imgMax2=imgMax2-imgs.length;
		var jsonImgs = [];
		var temp = [];
		for (var j = 0; j < imgs.length; j++) {
			temp.push(vo.fileDomain+imgs[j]);
			jsonImgs.push({
				imgPath : vo.fileDomain+imgs[j],
			});
		}
		oldImg = temp.join(";");
		listdataImg.addData(jsonImgs);
		imgGridView.refreshItems();
	})
}

do_Button.on("touch", function(data, e) {
	page.hideKeyboard();
	//do_Button.enabled = false;
	var msg = [];
	if(txtRemarkText.text.trim()==""){
		msg.push("请填写技能");
	}
	if (listdataImg.getCount() == 0) {
		msg.push("请添加图片");
	} else if (listdataImg.getCount() > imgMax) {
		msg.push("不能多于" + imgMax + "张图片");
	}
	if (msg.length > 0) {
		//do_Button.enabled = true;
		nf.toast(msg.toString());
		return;
	}
	var imgFilesOld = [];
	var imgFilesNew = [];
	for (var i = 0; i < listdataImg.getCount(); i++) {
		var imgPath = listdataImg.getOne(i).imgPath;
		if (oldImg.indexOf(imgPath) < 0) {// 不是原图片
			imgFilesNew.push(imgPath);
		} else {
			imgFilesOld.push(imgPath.replace(fileDomain, ""));
		}
	}
	var selectItem = [];
	for (var i=0; i<addItems.length; i++) {
		var addItem = addItems[i];
		var do_CheckBox_1 = ui(addItem.addItemUi+".do_CheckBox_1");
		if (do_CheckBox_1.checked) {
			var homemakingItemVo = {};
			homemakingItemVo.itemId = addItem.itemId;
			if (typeParentId != homemakingUtil.typeId.JiaDianWeiXiu) { //不是家电维修免费
				var do_TextField_price = ui(addItem.addItemUi+".do_TextField_price");
				var price = do_TextField_price.text.trim()
				homemakingItemVo.price = price;
			}
			selectItem.push(homemakingItemVo);
		}
	}
	param.homemakingItemVo = selectItem;
	param.name = txtRemarkText.text.trim();
	if (imgFilesNew.length > 0) {
		qqHttp.form(imgFilesNew,"homemaking",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			deviceone.print("-------string4imgs---------"+string4imgs);
			if(string4imgs==""){
				//do_Button.enabled = true;
				nf.toast("请添加图片");
				return;
			}
			imgFilesOld.push(string4imgs);
			param.img = imgFilesOld.join(";");
			deviceone.print("提交数据："+JSON.stringify(param));
			nf.confirm({text:"是否确认提交?",title:"提示",button1text:"提交",button2text:"取消"},function(datacall,e){
				if(datacall == 1){
					qqHttp.post(homemakingUtil.url.base.edit, param, function(data) {
						nf.toast("编辑成功");
						app.closePage();
					});
				} else {
					//do_Button.enabled = true;
				}
			});
		});
	} else {
		param.img = imgFilesOld.join(";");
		deviceone.print("提交数据："+JSON.stringify(param));
		nf.confirm({text:"是否确认提交?",title:"提示",button1text:"提交",button2text:"取消"},function(datacall,e){
			if(datacall == 1){
				qqHttp.post(homemakingUtil.url.base.edit, param, function(data) {
					nf.toast("编辑成功");
					app.closePage();
				});
			}
		});
	}
});

//接受地图页面关闭的时候传递回来的数据
page.on("result", function(data) {
})
ui("do_ScrollView_1").redraw();
//点击背景隐藏输入键盘
rootview.on("touch",function(){
	page.hideKeyboard();
})